1147D - Palindrome XOR - CodeForces Solution


dfs and similar graphs *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N=2005,mod=998244353;
int a[N],b[N],pa[N],w[N],p[N],n,m,ans;
char s[N]; 
inline int getpa(int x)
{
	if(pa[x]==x)
		return x;
	int p=getpa(pa[x]);
	w[x]^=w[pa[x]];
	return pa[x]=p;
}
inline int link(int a,int b,int c)
{
	if(getpa(a)!=getpa(b))
	{
		w[pa[a]]=w[a]^w[b]^c;
		pa[pa[a]]=pa[b];
	}
	else if(w[a]^w[b]^c)
		return 0;
	return 1;
}
int solve(int A,int B)
{
	memset(pa,0,sizeof(pa));
	memset(w,0,sizeof(w));
	for(int i=1;i<=B*2+3;i++)
		pa[i]=i;
	m=0;
	int zero=++m;
	for(int i=0;i<=B;i++)
		a[i]=++m,(i>A?link(zero,m,0):0);
	for(int i=0;i<=B;i++)
		b[i]=++m;
	link(zero,a[0],1),link(zero,b[0],1);
	for(int i=0,j=A;i<j;i++,j--)
		if(!link(a[i],a[j],0))
			return 0;
	for(int i=0,j=B;i<j;i++,j--)
		if(!link(b[i],b[j],0))
			return 0;
	for(int i=0;i<=B;i++)
		if(s[i]!='?'&&!link(a[i],b[i],s[i]-'0'))
			return 0;
	int owo=0;
	for(int i=1;i<=m;i++)
		owo+=(pa[i]==i);
	return p[owo-1];
}
int main()
{
	scanf("%s",s);
	n=strlen(s);
	reverse(s,s+n);
	p[0]=1;
	for(int i=1;i<=n*2;i++)
		p[i]=p[i-1]*2%mod;
	for(int i=1;i<n;i++)
		ans=(ans+solve(i-1,n-1))%mod;
	printf("%d\n",ans);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers